package com.wuhunyu.advice;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

/**
 * 登录通知
 *
 * @author wuhunyu
 * @version 1.0
 * @date 2021-01-27 15:56
 */
@Aspect
@Component
public class LoginAdvice {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登录请求切入点表达式
     */
    @Pointcut("execution(* com.wuhunyu.controller.UcenterMemberController.login(..))")
    public void point() {}

    @After("point()")
    public void countLogin() {
        // 判断redis中是否已经存在login字段
        if (redisTemplate.opsForValue().get("login") == null) {
            // 创建一个login
            redisTemplate.opsForValue().set("login", "1", 1, TimeUnit.DAYS);
            return ;
        }
        // 已存在login字段，执行 +1 操作
        redisTemplate.opsForValue().increment("login", 1);
    }

}
